#include "Event.h"




void TEventDistpacher::addEventListener(string event ,TEventHandel* handel){
    if(event.size()==0 || handel==NULL){
        return;
    } 
    
    if(handelMap.find(event)==handelMap.end()){
        vector<TEventHandel*> handels;
        handelMap[event]=handels;
    }
    
    vector<TEventHandel*>& curHandels= handelMap[event];
    for(int i=0;i<curHandels.size();i++) {
        if(curHandels[i]==handel){
            return;
        }
    }
    curHandels.push_back(handel);
    
}


void TEventDistpacher::removeEventListener(string event ,TEventHandel* handel){
    if(event.size()==0 || handel==NULL){
        return;
    }
    
    if(handelMap.find(event)==handelMap.end()){
        return;
    }
    
    vector<TEventHandel*>& curHandels= handelMap[event];
    vector<TEventHandel*>::iterator it=curHandels.begin();
    
    for(;it!=curHandels.end();it++) {
        if(*it==handel){
            curHandels.erase(it);
            return;
        }
    }
    
    
}
void TEventDistpacher::dispatcherEvent(string event){
    
    if(handelMap.find(event)!=handelMap.end()){
        vector<TEventHandel*>& curHandels= handelMap[event];
        
        vector<TEventHandel*>::iterator it=curHandels.begin();
        
        for(;it!=curHandels.end();it++) {
            (*it)->doEvent(event,this);
        }
    }
}
